﻿using Itech_Run.Domain.Shared.Const;
using Itech_Run.Domain.Shared.Exception;
using Itech_Run.Domain.Shared.Localization.Back;
using Itech_Run.Sys.Domain.Entity;
using Microsoft.Extensions.Localization;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.Domain.Services;

namespace Itech_Run.Sys.Domain.Manager
{
    public class Sys_RoleManager : DomainService
    {
        private readonly IRepository<Sys_Role, string> _roleRepository;
        private readonly IStringLocalizer<BackResource> _localizer;
        public Sys_RoleManager(IRepository<Sys_Role, string> roleRepository, IStringLocalizer<BackResource> localizer)
        {
            _roleRepository = roleRepository;
            _localizer = localizer;
        }

        public async Task Check_UniqueAsync(Sys_Role entity)
        {
            await Check_UniqueNameAsync(entity.Id, entity.Name);
        }

        private async Task Check_UniqueNameAsync(string id, string name)
        {
            if ((await _roleRepository.GetQueryableAsync()).Where(p => p.Name == name && p.Id != id).Any())
                throw new CustomFriendlyException(BackErrorConst.RoleNameAlreadyExists, _localizer[BackErrorConst.RoleNameAlreadyExists]);
        }
    }
}
